package org.paradise.dms.web.action.message;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.MessageOut;
import org.paradise.dms.services.MessageOutService;
import org.paradise.dms.util.MainSmsDemo1;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.DWRPage;
import org.paradise.dms.web.tools.Pager;
import org.paradise.dms.web.tools.PagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * Description: 系统用户以及用户组的CRUD管理Action
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 2009-3-17 上午12:05:56 李双江（paradise.lsj@gmail.com）created
 */
@Service
@RemoteProxy(name = "messageAction")
public class MessageAction extends DMSBaseAction {

	/**
	 * 
	 */
	@SuppressWarnings("unused")
	private static final long serialVersionUID = -3438968354104415561L;
	private static Logger log = Logger.getLogger(MessageAction.class);
 	@Autowired
	private MessageOutService messageOutService;
 	@Autowired
	private PagerService pagerService;
 	private Pager pager;
	protected String currentPage;
	protected String totalRows;
	protected String pagerMethod;
	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(String totalRows) {
		this.totalRows = totalRows;
	}

	public String getPagerMethod() {
		return pagerMethod;
	}

	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}

	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public MessageOutService getMessageOutService() {
		return messageOutService;
	}

	public void setMessageOutService(MessageOutService messageOutService) {
		this.messageOutService = messageOutService;
	}

	private MessageOut messageOut;
	private String receivertelephone;
	private String messagecontent;
	private int messagestate;
	private String messagetime;
	private String messagereason;
	private  String sendId;
	private String submitReserve;
	private String msgContent;
	private String reportState;
	private List<Object> slist_id = new ArrayList<Object>();


	public List<Object> getSlist_id() {
		return slist_id;
	}

	public void setSlist_id(List<Object> slist_id) {
		this.slist_id = slist_id;
	}

	public String getReceivertelephone() {
		return receivertelephone;
	}

	public void setReceivertelephone(String receivertelephone) {
		this.receivertelephone = receivertelephone;
	}

	public String getMessagecontent() {
		return messagecontent;
	}

	public void setMessagecontent(String messagecontent) {
		this.messagecontent = messagecontent;
	}

	public int getMessagestate() {
		return messagestate;
	}

	public void setMessagestate(int messagestate) {
		this.messagestate = messagestate;
	}

	public String getMessagetime() {
		return messagetime;
	}

	public void setMessagetime(String messagetime) {
		this.messagetime = messagetime;
	}

	public String getMessagereason() {
		return messagereason;
	}

	public void setMessagereason(String messagereason) {
		this.messagereason = messagereason;
	}
//发送短信
	public String message() {

		try {
			MainSmsDemo1 mainSmsDemo1 = new MainSmsDemo1 ();
			mainSmsDemo1.message(this.getReceivertelephone(), this
					.getMessagecontent());
			return SUCCESS;

		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}
//发件箱列表
	@SuppressWarnings("unchecked")
	public String messageList() {
		try {
			int totalRow = this.messageOutService.getMessageOutRows(); // 这里在service层写一个查询所有结果ROWS的方法。
			pager = pagerService.getPager(this.getCurrentPage(), this.getPagerMethod(), totalRow);
			this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
			this.setTotalRows(String.valueOf(totalRow));
			slist_id = this.messageOutService.getMessageList(pager.getPageSize(), pager.getStartRow());
			log.info("发件箱数目" + slist_id.size());			
			return SUCCESS;
		} catch (Exception e) {
			log.info("DMS_error:发件箱列表有误！");
			log.info("DMS_error:错误原因！" + e);
			return INPUT;
		}
	}

	public MessageOut getMessageOut() {
		return messageOut;
	}

	public void setMessageOut(MessageOut messageOut) {
		this.messageOut = messageOut;
	}
//验证电话
	public String delete86(String param) {
		if (param.indexOf("86") == 0)
			return param.substring(2);
		else if (param.indexOf("+86") == 0)
			return param.substring(3);
		else
			return param;
	}

	public String validPhoneNos(String PhoneNos) {
		String[] PhoneNo = PhoneNos.split("=");

		String Phone = PhoneNo[1];
		String[] phoneNos = Phone.split(",");
		int unvalidNo = 0;
		for (int i = 0; i < phoneNos.length; i++) {
			phoneNos[i] = delete86(phoneNos[i]);
			if (phoneNos[i].length() != 11) {
				unvalidNo++;
			}
		}
		String ss = "您输入了" + phoneNos.length + "个手机号，其中" + unvalidNo
				+ "个非法的手机号";
		return ss;

	}

	/**
	 * 
	 * Description: 获取发件箱总行数记录
	 * 
	 * @Version1.0 Apr 16, 2009 1:11:04 PM 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public int getMessageTotalRows() {
		return messageOutService.getMessageOutRows();
	}

	/**
	 * 
	 * Description:返回DWR页面分页对象
	 * 
	 * @param session
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONObject getPage(HttpSession session) {
		JSONObject joa = new JSONObject().fromObject((DWRPage) session
				.getAttribute("page"));
		return joa;
	}

	/**
	 * 
	 * Description: 根据id，删除一条发件信息，返回删除状态
	 * 
	 * @param apartmentid
	 * @return
	 */
	public String delelteMessageByID() {
		long mrid = Long.parseLong(ServletActionContext.getRequest().getParameter("id"));
		
		if (messageOutService.deleteMessage(mrid)!=null){
			return SUCCESS;
		}
		else
			return INPUT;
	}

	public String getSendId() {
		return sendId;
	}

	public void setSendId(String sendId) {
		this.sendId = sendId;
	}

	public String getSubmitReserve() {
		return submitReserve;
	}

	public void setSubmitReserve(String submitReserve) {
		this.submitReserve = submitReserve;
	}

	public String getMsgContent() {
		return msgContent;
	}

	public void setMsgContent(String msgContent) {
		this.msgContent = msgContent;
	}

	public String getReportState() {
		return reportState;
	}

	public void setReportState(String reportState) {
		this.reportState = reportState;
	}
}
